package learningtests;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;

import org.junit.Test;

public class ByteBufferLearning {

    @Test
    public void testByteBufferAllocateDirect() {
        StringBuilder builder = new StringBuilder("Testing ByteBuffer Allocating Direct\n");

        File file = new File("C:/test/av.index");

        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        FileChannel channel = fis.getChannel();

        ByteBuffer buffer = ByteBuffer.allocateDirect((int) file.length());

        try {
            while (channel.read(buffer) != -1) {

            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        byte[] bytes = new byte[500];

        buffer.get(bytes);

        String text = new String(bytes);

        builder.append(text);

        System.out.println(builder.toString());
    }
}
